package com.hua.yuan.live.view.holder_view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.hua.yuan.common.BaseHolderView;
import com.hua.yuan.live.R;
import com.hua.yuan.live.listener.OnBuyClickListener;
import com.hua.yuan.live.repertory.cloud_data.res.ResercationBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 直播列表的ITEM
 */
public class HolderView_Reservation implements BaseHolderView<ResercationBean>, View.OnClickListener {


    private TextView mTvTime;
    private TextView mTvPrice;
    private TextView mTvBuyState;


    private OnBuyClickListener mOnLiveClickListener;


    private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    private static final String TAG = HolderView_Reservation.class.getName();

    private int mPostion;


    public HolderView_Reservation(View pView, int pPosition) {

        mTvTime = pView.findViewById(R.id.tv_time);
        mTvPrice = pView.findViewById(R.id.tv_price);
        mTvBuyState = pView.findViewById(R.id.tv_buy_state);
        mTvBuyState.setTag(pPosition);
        mTvBuyState.setOnClickListener(this);
        Log.i(TAG, "positionn:=" + pPosition);
    }

    public void setOnBuyClickListener(OnBuyClickListener pOnLiveClickListener) {

        this.mOnLiveClickListener = pOnLiveClickListener;
    }

    @SuppressLint({"StringFormatInvalid", "StringFormatMatches"})
    @Override
    public void bindHolder(@NonNull List<ResercationBean> pList, int position, @NonNull Context pContext) {
        this.mPostion = position;
        String current = df.format(System.currentTimeMillis());
        String startTime = "";
        String endTime = "";

        if (pList == null || pList.size() == 0 || (position > pList.size())) {
            return;
        }

        ResercationBean resercationBean = pList.get(position);
        if (resercationBean == null) {
            return;
        }

        startTime = resercationBean.getStartTime();
        endTime = resercationBean.getEndTime();

        if (!TextUtils.isEmpty(startTime) && (!TextUtils.isEmpty(endTime))) {
            mTvTime.setText(String.format("今天  %s - %s", startTime, endTime));
        } else {
            return;
        }
        mTvPrice.setText(String.valueOf(resercationBean.getMoney()));
        /**
         * 如果直播过期则显示过期
         */
        if (resercationBean.getIsPass() == 1) {
            mTvBuyState.setText(pContext.getResources().getString(R.string.end_resevation));
            return;
        }
        //判断是否是当前用户预约过了
        if (resercationBean.getIsUserAppointment() == 1) {
            //如果是当前用户预约过了，则显示开始直播
            if (isOpenVideo(startTime, endTime)) {
                mTvBuyState.setText(pContext.getResources().getString(R.string.begin_resevation));
                return;
            } else {
                mTvBuyState.setText(pContext.getResources().getString(R.string.aleardy_resevation));
                return;
            }
        } else if (resercationBean.getHasAppointment() == 1) {
            mTvBuyState.setText(pContext.getResources().getString(R.string.wait_resevation));
            return;
        }
        mTvBuyState.setText(pContext.getResources().getString(R.string.no_resevation));

    }

    @Override
    public void onClick(View v) {

        if (R.id.tv_buy_state == v.getId()) {
            mOnLiveClickListener.click(this.mPostion);
        }
    }

    /**
     * 是否可以点击开始视频
     *
     * @param startTime
     * @param endTime
     * @return
     */
    /**
     * 是否可以点击开始视频
     *
     * @param startTime
     * @param endTime
     * @return
     */
    private boolean isOpenVideo(String startTime, String endTime) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String currentDate = df.format(System.currentTimeMillis());
        String startDateTime = String.format("%s %s", currentDate, startTime);
        String endDateTime = String.format("%s %s", currentDate, endTime);
        String currentTime = sf.format(new Date());
        //已预约
        try {
            long dStarTime = sf.parse(startDateTime).getTime();
            long dEndTime = sf.parse(endDateTime).getTime();
            long current=sf.parse(currentTime).getTime();
            if (current >= dStarTime && current <= dEndTime) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;

    }

}
